package service.impl;

import bean.Student;
import dao.StudentDao;
import exception.UserRepeatException;
import service.StudentService;

import java.util.List;

/**
 * @author HLF
 * @version V1.0
 * @package service.impl
 * @date 2021/10/24 19:51
 */
public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao;

    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }

    @Override
    public int registStudent(Student student) {
        Integer existStudent = existStudent(student.getId());
        int i = 0;
        if (existStudent == 0) {//判断如果是该账号未注册
            i = studentDao.insertStudent(student);
        } else {
            //throw new UserRepeatException("该用户id已被注册！");
        }
        return i;
    }

    @Override
    public List<Student> queryStudents() {
        List<Student> list = studentDao.selectStudents();
        return list;
    }

    @Override
    public Integer existStudent(Integer id) {
        Student student = studentDao.selectStudentById(id);
        if (student == null) {
            return 0;//表示未注册
        }
        return 1;//表示已注册
    }
}
